﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using FrameWork.Components;

namespace FrameWork.web.Module.IntegralMS.UserSelect
{
    public partial class Dept : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region 接收参数ViewState

                string[] allkeys = Request.QueryString.AllKeys;
                for (int i = 0; i < allkeys.Length; i++)
                {
                    ViewState[allkeys[i]] = Request[allkeys[i]];
                }
                #endregion
                LoadTree();
            }
        }
        //显示树图
        private void LoadTree()
        {
            this.TreeView1.Nodes.Clear();
            QueryParam qp = new QueryParam(1, 5);
            int RecordCount = 0;
            qp.Where = "Where G_Delete=0";
            qp.Orderfld = "G_Level,G_ShowOrder";
            qp.OrderType = 0;
            ArrayList list = BusinessFacade.sys_GroupList(qp, out RecordCount);
            foreach (sys_GroupTable item in list)
            {
                string groupID = item.GroupID.ToString();
                string groupName = item.G_CName.ToString();
                int G_ChildCount = item.G_ChildCount;
                TreeNode node = new TreeNode();
                node.Value = groupID;
                node.Text = groupName;
                node.Target = "user";
                node.NavigateUrl = "User.aspx?ToId=1" + ViewState["ToId"] + "&ToName=1" + ViewState["ToName"] + "&GroupID=" + groupID + "&GroupName=" + groupName;
                node.SelectAction = TreeNodeSelectAction.Select;
                TreeView1.Nodes.Add(node);
                if (G_ChildCount > 0) LoadNode(node, item.G_ParentID);
            }
        }
        //显示子单位
        private void LoadNode(TreeNode mainNode, int parentID)
        {
            QueryParam qp = new QueryParam(1, 5);
            int RecordCount = 0;
            qp.Where = string.Format("Where G_Delete=0 AND G_ParentID={0}", parentID);
            qp.Orderfld = "G_Level,G_ShowOrder";
            qp.OrderType = 0;
            ArrayList list = BusinessFacade.sys_GroupList(qp, out RecordCount);
            foreach (sys_GroupTable item in list)
            {
                string groupID = item.GroupID.ToString();
                string groupName = item.G_CName.ToString();
                int G_ChildCount = item.G_ChildCount;
                TreeNode node = new TreeNode();
                node.Value = groupID;
                node.Text = groupName;
                node.Target = "user";
                node.NavigateUrl = "User.aspx?ToId=" + ViewState["ToId"] + "&ToName=" + ViewState["ToName"] + "&GroupID=" + groupID + "&GroupName=" + groupName;
                node.SelectAction = TreeNodeSelectAction.Select;
                mainNode.ChildNodes.Add(node);
                if (G_ChildCount > 0) LoadNode(node, item.G_ParentID);
            }

        }
    }
}